/*==================================================
 *  Default Event Source
 *==================================================
 */


Timeline.DefaultEventSource = function(eventIndex) {
    this._events = (eventIndex instanceof Object) ? eventIndex : new Timeline.EventIndex();
    this._listeners = [];
};

Timeline.DefaultEventSource.prototype.addListener = function(listener) {
    this._listeners.push(listener);
};

Timeline.DefaultEventSource.prototype.removeListener = function(listener) {
    for (var i = 0; i < this._listeners.length; i++) {
        if (this._listeners[i] == listener) {
            this._listeners.splice(i, 1);
            break;
        }
    }
};

Timeline.DefaultEventSource.prototype.loadXML = function(xml, url) {
    var base = this._getBaseURL(url);
    
    var dateTimeFormat = xml.documentElement.getAttribute("date-time-format");
    if (dateTimeFormat != null) {
        dateTimeFormat = dateTimeFormat.toLowerCase();
    }
    var parseDateTimeFunction =
        (dateTimeFormat == "iso8601" || dateTimeFormat == "iso 8601") ?
        Timeline.DateTime.parseIso8601DateTime : 
        Timeline.DateTime.parseGregorianDateTime;

    var node = xml.documentElement.firstChild;
    var added = false;
    while (node != null) {
        if (node.nodeType == 1) {
            var description = "";
            if (node.firstChild != null && node.firstChild.nodeType == 3) {
                description = node.firstChild.nodeValue;
            }
            var evt = new Timeline.DefaultEventSource.Event(
                parseDateTimeFunction(node.getAttribute("start")),
                parseDateTimeFunction(node.getAttribute("end")),
                parseDateTimeFunction(node.getAttribute("latestStart")),
                parseDateTimeFunction(node.getAttribute("earliestEnd")),
                node.getAttribute("isDuration") != "true",
                node.getAttribute("title"),
                description,
                this._resolveRelativeURL(node.getAttribute("image"), base),
                this._resolveRelativeURL(node.getAttribute("link"), base),
                this._resolveRelativeURL(node.getAttribute("icon"), base),
                node.getAttribute("color"),
                node.getAttribute("textColor")
            );
            this._events.add(evt);
            
            added = true;
        }
        node = node.nextSibling;
    }

    if (added) {
        for (var i = 0; i < this._listeners.length; i++) {
            this._listeners[i].onAddMany();
        }
    }
};


Timeline.DefaultEventSource.prototype.loadJSON = function(data, url) {
    var base = this._getBaseURL(url);
    var added = false;  
   if (data && data.events){
       dateTimeFormat = (data.dateTimeFormat && data.dateTimeFormat.toLowerCase()) || "iso8601";
       
       var parseDateTimeFunction =
           (dateTimeFormat == "iso8601" || dateTimeFormat == "iso 8601") ?
           Timeline.DateTime.parseIso8601DateTime : 
           Timeline.DateTime.parseGregorianDateTime;
   
       for (var i=0; i < data.events.length; i++){
           var event = data.events[i];
           var evt = new Timeline.DefaultEventSource.Event(
                   parseDateTimeFunction(event.start),
                   parseDateTimeFunction(event.end),
                   parseDateTimeFunction(event.latestStart),
                   parseDateTimeFunction(event.earliestEnd),
                   event.isDuration || false,
                   event.title,
                    event.description,
                   this._resolveRelativeURL(event.image, base),
                   this._resolveRelativeURL(event.link, base),
                   this._resolveRelativeURL(event.icon, base),
                   event.color,
                   event.textColor
           );
           this._events.add(evt);
           added = true;
       }
   }
   
    if (added) {
        for (var i = 0; i < this._listeners.length; i++) {
            this._listeners[i].onAddMany();
        }
    }
};

Timeline.DefaultEventSource.prototype.LoadDataTable = function()
{
		var icons = ["Addressbook.ico",
					 "finder.ico",
					 "garageband.ico",
					 "ical.ico",
					 "iChat.ico",
					 "imovie.ico",
					 "iphoto.ico",
					 "itunes.ico",
					 "quicktime.ico",
					 "safari.ico" ];
		
		for(var i=0; i<10; i++)
		{
			var evt = new Timeline.DefaultEventSource.Event(
			 Timeline.DateTime.parseGregorianDateTime("Mon July 17 2006 12:30:00 GMT-0400"),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 true,
			 "12:30 Meetings"+(i+1),
			 "Remember to go to the 12:30 Meeting ",
			 "http://www.favorite-puppy-names.com/images/cocker_spaniel_puppy_picture.jpg",
			 "http://www.limingxu.com",
			 "images/icons/"+icons[i],
		 	  "#CC9B9B",
			 "#CC9B9B"
			);
			
			this._events.add(evt);
		
		}
		
		for (var i = 0; i < this._listeners.length; i++) {
            this._listeners[i].onAddMany();
        }
}

Timeline.DefaultEventSource.prototype.LoadEvent= function(taskname,taskdesc,startdate,starttime)
{
		
		    var start1 = String(startdate)+" "+String(starttime)+ " GMT-0400";
			
			var evt = new Timeline.DefaultEventSource.Event(
			 Timeline.DateTime.parseGregorianDateTime(start1),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 true,
			 taskname,
			 taskdesc,
			 "",
			 "",
			 "",
			 "#CC9B9B",
			 "#CC9B9B"
			);
			
			this._events.add(evt);
		
		
		
		for (var i = 0; i < this._listeners.length; i++) {
            this._listeners[i].onAddMany();
        }
}

Timeline.DefaultEventSource.prototype.LoadEventComplete= function(startdate,starttime, enddate,endtime,latestStart,earliestEnd,isDuration,taskname,taskdesc,eventimage,eventurl,eventicon,eventcolor,eventtextcolor)
{
		
		    var start1 = String(startdate)+" "+String(starttime);
			var mystart = new Date(start1);
			//alert(mystart);
			var myend
			if(enddate!=null && enddate!="")
			{
				var end1 = String(enddate)+ " "+String(endtime);
				myend = new Date(end1);
				//alert(myend);
			}
			else{
				//alert("enddate is null");
			}
			
			if(eventimage==null){
				eventimage="";
			}
			if(eventurl==null){
				eventurl="";
			}
			if(eventicon==null){
				eventicon="";
			}
			if(eventcolor==null){
				eventcolor="";
			}
			else{
				eventcolor = "#"+eventcolor;
			}
			if(eventtextcolor==null){
				eventtextcolor="";
			}
			
		
			var evt = new Timeline.DefaultEventSource.Event(
			 Timeline.DateTime.parseGregorianDateTime(mystart),
			 Timeline.DateTime.parseGregorianDateTime(myend),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 Timeline.DateTime.parseGregorianDateTime(null),
			 isDuration,
			 taskname,
			 taskdesc,
			 eventimage,
			 eventurl,
			 eventicon,
			 eventcolor,
			 eventtextcolor
			);
			this._events.add(evt);
		
		
		
		for (var i = 0; i < this._listeners.length; i++) {
            this._listeners[i].onAddMany();
        }
}

Timeline.DefaultEventSource.prototype.add = function(evt) {
    this._events.add(evt);
    for (var i = 0; i < this._listeners.length; i++) {
        this._listeners[i].onAddOne(evt);
    }
};

Timeline.DefaultEventSource.prototype.clear = function() {
    this._events.removeAll();
    for (var i = 0; i < this._listeners.length; i++) {
        this._listeners[i].onClear();
    }
};

Timeline.DefaultEventSource.prototype.getEventIterator = function(startDate, endDate) {
    return this._events.getIterator(startDate, endDate);
};

Timeline.DefaultEventSource.prototype.getAllEventIterator = function() {
    return this._events.getAllIterator();
};

Timeline.DefaultEventSource.prototype.getCount = function() {
    return this._events.getCount();
};

Timeline.DefaultEventSource.prototype.getEarliestDate = function() {
    return this._events.getEarliestDate();
};

Timeline.DefaultEventSource.prototype.getLatestDate = function() {
    return this._events.getLatestDate();
};

Timeline.DefaultEventSource.prototype._getBaseURL = function(url) {
    if (url.indexOf("://") < 0) {
        var url2 = this._getBaseURL(document.location.href);
        if (url.substr(0,1) == "/") {
            url = url2.substr(0, url2.indexOf("/", url2.indexOf("://") + 3)) + url;
        } else {
            url = url2 + url;
        }
    }
    
    var i = url.lastIndexOf("/");
    if (i < 0) {
        return "";
    } else {
        return url.substr(0, i+1);
    }
};

Timeline.DefaultEventSource.prototype._resolveRelativeURL = function(url, base) {
    if (url == null || url == "") {
        return url;
    } else if (url.indexOf("://") > 0) {
        return url;
    } else if (url.substr(0,1) == "/") {
        return base.substr(0, base.indexOf("/", base.indexOf("://") + 3)) + url;
    } else {
        return base + url;
    }
};


Timeline.DefaultEventSource.Event = function(
        start, end, latestStart, earliestEnd, instant, 
        text, description, image, link,
        icon, color, textColor) {
        
    this._id = "e" + Math.floor(Math.random() * 1000000);
    
    this._instant = instant || (end == null);
    
    this._start = start;
    this._end = (end != null) ? end : start;
    
    this._latestStart = (latestStart != null) ? latestStart : (instant ? this._end : this._start);
    this._earliestEnd = (earliestEnd != null) ? earliestEnd : (instant ? this._start : this._end);
    
    this._text = text;
    this._description = description;
    this._image = (image != null && image != "") ? image : null;
    this._link = (link != null && link != "") ? link : null;
    
    this._icon = (icon != null && icon != "") ? icon : null;
    this._color = (color != null && color != "") ? color : null;
    this._textColor = (textColor != null && textColor != "") ? textColor : null;

};

Timeline.DefaultEventSource.Event.prototype = {
    getID:          function() { return this._id; },
    
    isInstant:      function() { return this._instant; },
    isImprecise:    function() { return this._start != this._latestStart || this._end != this._earliestEnd; },
    
    getStart:       function() { return this._start; },
    getEnd:         function() { return this._end; },
    getLatestStart: function() { return this._latestStart; },
    getEarliestEnd: function() { return this._earliestEnd; },
    
    getText:        function() { return this._text; },
    getDescription: function() { return this._description; },
    getImage:       function() { return this._image; },
    getLink:        function() { return this._link; },
    
    getIcon:        function() { return this._icon; },
    getColor:       function() { return this._color; },
    getTextColor:   function() { return this._textColor; },
    
    fillDescription: function(elmt) {
        elmt.innerHTML = this._description;
    },
    fillTime: function(elmt, labeller) {
        if (this._instant) {
            if (this.isImprecise()) {
                elmt.appendChild(elmt.ownerDocument.createTextNode(labeller.labelPrecise(this._start)));
                elmt.appendChild(elmt.ownerDocument.createElement("br"));
                elmt.appendChild(elmt.ownerDocument.createTextNode(labeller.labelPrecise(this._end)));
            } else {
                elmt.appendChild(elmt.ownerDocument.createTextNode(labeller.labelPrecise(this._start)));
            }
        } else {
            if (this.isImprecise()) {
                elmt.appendChild(elmt.ownerDocument.createTextNode(
                    labeller.labelPrecise(this._start) + " ~ " + labeller.labelPrecise(this._latestStart)));
                elmt.appendChild(elmt.ownerDocument.createElement("br"));
                elmt.appendChild(elmt.ownerDocument.createTextNode(
                    labeller.labelPrecise(this._earliestEnd) + " ~ " + labeller.labelPrecise(this._end)));
            } else {
                elmt.appendChild(elmt.ownerDocument.createTextNode(labeller.labelPrecise(this._start)));
                elmt.appendChild(elmt.ownerDocument.createElement("br"));
                elmt.appendChild(elmt.ownerDocument.createTextNode(labeller.labelPrecise(this._end)));
            }
        }
    }
};